using System;
using System.Windows.Forms;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestDMUniqueConstraintSelectorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        [Test]
        public void Test_NullControlFactory_RaisesError()
        {
            //--------------- Set up test pack ------------------
            
            //--------------- Test Preconditions ----------------

            //--------------- Execute Test ----------------------
            try
            {
                DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(null);
                Assert.Fail("expected Err");
            }
                //---------------Test Result -----------------------
            catch (HabaneroArgumentException ex)
            {
                StringAssert.Contains("Cannot create an 'DMUniqueConstraintSelectorControl' if the control factory is null", ex.Message);
            }
            //--------------- Test Result -----------------------

        }

//        //[Ignore("Changing how this works")] 
        [Test]
        public void Test_Construction()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.Controls.Count);
            Assert.IsInstanceOf(typeof (IGridControl), control.Controls[0]);
            IGridControl gridControl = (IGridControl) control.Controls[0];
            IButtonGroupControl buttonsControl = gridControl.Buttons;
            Assert.IsTrue(buttonsControl.Visible);
            Assert.IsFalse(gridControl.FilterControl.Visible);
            Assert.AreEqual(3, buttonsControl.Controls.Count);
            Assert.IsNotNull(buttonsControl["AddProperties"]);
            Assert.IsNotNull(buttonsControl["Save"]);
            Assert.IsFalse(buttonsControl["Save"].Visible);
            Assert.IsFalse(buttonsControl["AddProperties"].Enabled);
        }

        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------

            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetDMClass()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithUniqueConstraints();
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreEqual(dmClass.UniqueConstraints.Count, control.BusinessObjectCollection.Count);
            Assert.AreEqual(dmClass.UniqueConstraints.Count, control.UniqueConstraints.Count);
        }
        [Test]
        public void TestSetDMClass_Null_ClearsCollectionAndSelectedUniqueConstraint()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithUniqueConstraints();
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            control.Class = dmClass;
            //---------------Assert Precondition----------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreEqual(dmClass.UniqueConstraints.Count, control.BusinessObjectCollection.Count);
            Assert.AreEqual(dmClass.UniqueConstraints.Count, control.UniqueConstraints.Count);
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsNull(control.Class);
            Assert.IsNull(control.SelectedUniqueConstraint);
            Assert.AreEqual(0,control.BusinessObjectCollection.Count);
        }

        [Test]
        public void Test_DmClass_ShouldEnableSelector()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            //---------------Assert Preconditions---------------
            Assert.IsFalse(control.Enabled);
            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
            Assert.AreEqual(0, dmClass.UniqueConstraints.Count);
            Assert.IsFalse(control.GridControl.Buttons["AddProperties"].Enabled);

        }

        [Test]
        public void TestSetToNullDisables()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            control.Class = dmClass;
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetBusinessObject()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMUniqueConstraint uniqueConstraint = TestUtilsDMUniqueConstraint.CreateUnsavedValidDMUniqueConstraint(dmClass);
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            control.Class = dmClass;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.SelectedUniqueConstraint = uniqueConstraint;
            //---------------Test Result -----------------------
            Assert.AreSame(uniqueConstraint, control.SelectedUniqueConstraint);
            Assert.AreSame(control.SelectedUniqueConstraint, control.GridControl.SelectedBusinessObject);
            Assert.AreSame(control.SelectedUniqueConstraint, control.SelectedBusinessObject);
            Assert.IsTrue(control.GridControl.Buttons["AddProperties"].Enabled);
        }

        [Test]
        public void TestBusinessObjectSelectedEvent()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMUniqueConstraint uniqueConstraint = TestUtilsDMUniqueConstraint.CreateUnsavedValidDMUniqueConstraint(dmClass);
            DMUniqueConstraintSelectorControl control = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            bool fired = false;
            DMUniqueConstraint selected = null;
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            control.UniqueConstraintSelected += (sender, e) =>
            {
                fired = true;
                selected = e.BusinessObject;
            };
            control.SelectedUniqueConstraint = uniqueConstraint;
            //---------------Test Result -----------------------

            Assert.IsTrue(fired);
            Assert.AreSame(selected, control.SelectedUniqueConstraint);
            //---------------Tear Down -------------------------          
        }



//        //[Ignore("Changing how this works")] 
//        [Test]
//        public void TestCancelButton_ClickRestoresSavedObject()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            TestUtilsShared.CreateSavedDMProperty(dmClass);
//            BusinessObjectCollection<DMProperty> classes = dmClass.Properties;
//            DMUniqueConstraintSelectorControl propertySelectorControl = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = propertySelectorControl.GridControl;
//            propertySelectorControl.Class = dmClass;
//            DMProperty currentBO = (DMProperty) gridControl.SelectedBusinessObject;
//            currentBO.DisplayName = TestUtilsShared.GetRandomString();
//            //  ---------------Assert Precondition----------------
//            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(1, classes.Count);
//            Assert.IsNotNull(currentBO);
//            Assert.IsTrue(currentBO.Status.IsDirty);
//            //  ---------------Execute Test ----------------------
//            gridControl.Buttons["Cancel"].PerformClick();
//            // ---------------Test Result -----------------------
//            Assert.IsFalse(currentBO.Status.IsDirty);
//        }

//        [Test]
//        public void TestCancelButton_Click_NoObjecctSelected()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
//            BusinessObjectCollection<DMProperty> classes = dmClass.Properties;
//            DMUniqueConstraintSelectorControl propertySelectorControl = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
//            IGridControl gridControl = propertySelectorControl.GridControl;
//            propertySelectorControl.Class = dmClass;
//            DMProperty currentBO = (DMProperty) gridControl.SelectedBusinessObject;
//            //  ---------------Assert Precondition----------------
//            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(0, classes.Count);
//            Assert.IsNull(currentBO);
//            //  ---------------Execute Test ----------------------
//            gridControl.Buttons["Cancel"].PerformClick();
//            // ---------------Test Result -----------------------
//            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
//            Assert.AreEqual(0, classes.Count);
//        }

//        //[Ignore("Changing how this works")] 
////        [Test]
////        public void TestCancelButton_ClickRemovesNewObject_OnlyItemInGrid()
////        {
////            //---------------Set up test pack-------------------
////            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
////            TestUtilsShared.CreateSavedDMProperty(dmClass);
////            BusinessObjectCollection<DMProperty> classes = dmClass.Properties;
////            DMUniqueConstraintSelectorControl classSelectorControl = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
////            IGridControl gridControl = classSelectorControl.GridControl;
////            classSelectorControl.Class = dmClass;
////            IButton cancelButton = gridControl.Buttons["Cancel"];
////            IButton newButton = gridControl.Buttons["New"];
////
////            newButton.PerformClick();
////            //---------------Assert Precondition----------------
////            Assert.IsTrue(cancelButton.Enabled);
////            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
////            //---------------Execute Test ----------------------
////            cancelButton.PerformClick();
////            //---------------Test Result -----------------------
////            Assert.AreEqual(1, classes.Count, "The new item should be removed from the collection");
////            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
////        }/

//        //[Ignore("Changing how this works")] 
////        [Test]
////        public void TestCancelButton_ClickRemovesNewObject_TwoItemsInGrid()
////        {
////            // ---------------Set up test pack-------------------
////            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
////            TestUtilsShared.CreateSavedDMProperty(dmClass);
////            TestUtilsShared.CreateSavedDMProperty(dmClass);
////            DMUniqueConstraintSelectorControl classSelectorControl = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
////            IGridControl gridControl = classSelectorControl.GridControl;
////            classSelectorControl.Class = dmClass;
////            IButton cancelButton = gridControl.Buttons["Cancel"];
////            IButton newButton = gridControl.Buttons["New"];
////            newButton.PerformClick();
////            // ---------------Assert Precondition----------------
////            Assert.AreEqual(3, gridControl.Grid.Rows.Count);
////            //   ---------------Execute Test ----------------------
////            cancelButton.PerformClick();
////            //  ---------------Test Result -----------------------
////            Assert.AreEqual(2, gridControl.Grid.Rows.Count);
////            Assert.IsNotNull(gridControl.SelectedBusinessObject);
////        }

//        //[Ignore("Changing how this works")] 
//        //Need to figure out how to put a new item into datasetProvider/Grid
////        [Test]
////        public void Test_ObjectSavesWhenSaveButtonClicked()
////        {
////            //---------------Set up test pack-------------------
////            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
////            TestUtilsShared.CreateSavedDMProperty(dmClass);
////            DMUniqueConstraintSelectorControl classSelectorControl = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
////            IGridControl gridControl = classSelectorControl.GridControl;
////            classSelectorControl.Class = dmClass;
////            gridControl.Buttons["New"].PerformClick();
////            DMProperty currentBO = (DMProperty) gridControl.SelectedBusinessObject;
////            TestUtilsShared.UpdateDMPropertyWithValidData(currentBO);
////            currentBO.DisplayName = TestUtilsShared.GetRandomString();
////
////            //---------------Assert Precondition----------------
////            Assert.IsNotNull(currentBO);
////            Assert.IsTrue(currentBO.Status.IsNew);
////            Assert.IsTrue(currentBO.Status.IsDirty);
////            Assert.IsTrue(currentBO.IsValid());
////            //  ---------------Execute Test ----------------------
////            gridControl.Buttons["Save"].PerformClick();
////            // ---------------Test Result -----------------------
////            Assert.IsFalse(currentBO.Status.IsDirty);
////            Assert.IsFalse(currentBO.Status.IsNew);
////            Assert.IsFalse(currentBO.Status.IsDeleted);
////        }

//        //[Ignore("Changing how this works")] 
        [Test]
        public void Test_SaveButtonClicked_NoObjectSelected_DoesNotRaiseError()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClass();
            DMUniqueConstraintSelectorControl uniqueConstraintSelectorControl = new DMUniqueConstraintSelectorControl(new ControlFactoryWin());
            IGridControl gridControl = uniqueConstraintSelectorControl.GridControl;
            uniqueConstraintSelectorControl.Class = dmClass;
            DMProperty currentBO = (DMProperty)gridControl.SelectedBusinessObject;
            //---------------Assert Precondition----------------
            Assert.IsNull(currentBO);
            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
            //  ---------------Execute Test ----------------------
            gridControl.Buttons["Save"].PerformClick();
            // ---------------Test Result -----------------------
            Assert.AreEqual(1, gridControl.Grid.Rows.Count);
        }

   
    }

}